package com.google.android.apps.wakemewhere.ui;

import java.util.logging.Logger;

import android.content.Context;
import android.graphics.Canvas;
import android.location.Address;
import android.view.MotionEvent;

import com.google.android.apps.wakemewhere.AddressResolver;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;


class MapToastOverlay extends com.google.android.maps.Overlay {

	private static final Logger logger = Logger.getLogger(MapToastOverlay.class.getName());

	// It's easy to accidentally scroll immediately after moving the alarm.  This is very irritating.  Prevent it.
	private static final long PREVENT_SCROLL_AFTER_SET_TIME = 500;
	
	private final Context context;
	private final AddressResolver addressResolver;
	
	private GeoPoint geoPoint = null;
	private volatile int radius = 60;
	private volatile long lastMovedAlarmTime = Long.MIN_VALUE;

	public MapToastOverlay(Context context) {
		super();
		this.context = context;
		this.addressResolver = new AddressResolver(context);
	}
	
	public GeoPoint getGeoPoint(){
		return geoPoint;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
		int fingers = e.getPointerCount();
		if (fingers == 1 && System.currentTimeMillis() > (lastMovedAlarmTime + PREVENT_SCROLL_AFTER_SET_TIME)) {
			return super.onTouchEvent(e, mapView);
		} else if (fingers == 2) {
			int x1 = (int) e.getX(0);
			int y1 = (int) e.getY(0);
			int x2 = (int) e.getX(1);
			int y2 = (int) e.getY(1);
			int cx = (x1/2) + (x2/2);
			int cy = (y1/2) + (y2/2);
			int rx = Math.abs(x1 - cx);
			int ry = Math.abs(y1 - cy);
			int newRadius = (int) Math.sqrt(Math.pow(rx,2) + Math.pow(ry, 2));
			GeoPoint center = mapView.getProjection().fromPixels(cx, cy);
			logger.info("onTouchEvent x1=" + x1 + " y1=" + y1 + " x2=" + x2 + " y2=" + y2 + " cx=" + cx + " cy=" + cy + " rx=" + rx + " ry=" + ry + " radius=" + radius);
			logger.info("center geopoint=" + center);
			setAlarmArea(center, mapView, newRadius);
			return true;
		}
		return true;
	}
	
	// TODO(tcox):  Investigate putting onTap back for non-multitouch devices.  At the moment it doesn't work because the user takes
	// one of their two fingers off, resulting in a tap at the bottom left side of the circle they were dragging.
	
	//@Override
	boolean setAlarmArea(GeoPoint p, MapView mapView, int radius) {
		this.geoPoint = p;
		this.radius = radius;
		lastMovedAlarmTime = System.currentTimeMillis();
				
		for (Overlay overlay : mapView.getOverlays()) {
			if (overlay != this) {
				mapView.getOverlays().remove(overlay);
			}
		}

		// TODO(tcox): Talk to baran@ about this.  It's too slow to be synchronous with circle drawing.  It also obscures the
		// zoom buttons, which are necessary now because we have overridden pinch.
		//Address address = null;//addressResolver.getAddress(p);
		//if (address != null) {
		//	Toast.makeText(context, format(address), Toast.LENGTH_SHORT).show();
		//}
		
		RadiusOverlay overlay = new RadiusOverlay(p, radius);
		mapView.getOverlays().add(overlay);

		// move to location
		//mapView.getController().animateTo(p);

		// redraw map
		mapView.postInvalidate();
		return true;
	}

	private static String format(Address address) {
		int count = 0;
		boolean first = true;
		String out = "";
		String line = address.getAddressLine(count);
		while (line != null) {
			if (!first) {
				out += ", ";
			}
			out += line;
			count++;
			line = address.getAddressLine(count);
			first = false;
		}
		return out;
	}
}